﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using BartonExtension;
using Model;

namespace Backend
{
    public partial class FrmBrand : DevExpress.XtraEditors.XtraForm
    {
        public DialogResult result { get; set; }
        public Brand brand { get; set; }
        public FrmBrand()
        {
            InitializeComponent();
            result = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            if(txtCode.Text.isNullOrEmpty())
            {
                XtraMessageBox.Show("Empty code is not allowed");
                return;
            }

            if(txtTitle.Text.isNullOrEmpty())
            {
                XtraMessageBox.Show("Empty title is not allowed");
                return;
            }

            if(txtOriTitle.Text.isNullOrEmpty())
            {
                XtraMessageBox.Show("Empty title origin is not allowed");
                return;
            }

            if(txtLocation.Text.isNullOrEmpty())
            {
                XtraMessageBox.Show("Empty location is not allowed");
                return;
            }

            if(txtDesc.Text.isNullOrEmpty())
            {
                XtraMessageBox.Show("Empty description is not allowed");
                return;
            }

            this.brand = new Brand()
            {
                ID = -1,
                code = txtCode.Text,
                title = txtTitle.Text,
                title_origin = txtOriTitle.Text,
                level = spinLevel.EditValue.toFloat().Value,
                location = txtLocation.Text,
                desc = txtDesc.Text
            };

            //Validate
            int validate = DataSourcePool.brands.FindIndex(x =>
            {
                return x.code.Equals(brand.code);
            });
            if (validate != -1)
            {
                XtraMessageBox.Show("dumplicate brand data");
                return;
            }
            
            result = System.Windows.Forms.DialogResult.OK;
            this.Close();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            result = System.Windows.Forms.DialogResult.Cancel;
            this.Close();
        }
    }
}